import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;



public class Main {
	public static String path;	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println("USAGE:");
			System.out.println("Main <path>");
			System.out.println("");
			System.out.println("  path ... path to the distances lookup table files");
			System.out.println("");
		} else {
			System.out.println("");
			System.out.println("* * *  Welcome to our TTP program* * *");
			System.out.println("");
			new Main(args[0]);
		}
	}
	
	public Main(String path) {
		this.path = path;
		File file = new File(path);
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("File not found!");
		}
		String line;
		String lineArray[][] = null;
		
		//reading first line
		try {
			if((line = br.readLine()) != null)
			{
				//Removing extra spaces
				line = line.replaceAll("\\s+", " ");
				
				
				//Splitting rows
				String temp[] = line.trim().split(" ");
				
				//Initializing lineArray
				lineArray = new String[temp.length][temp.length];
				lineArray[0] = temp;
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			System.out.println("Can not read the line!");
		}
		
		int i=1; //Counter
		try {
			while ((line = br.readLine()) != null) {
				// process the current line
				
				//Removing extra spaces
				line = line.replaceAll("\\s+", " ");
				line=line.trim();
				
				if (!line.equals("")) {
					
					//Splitting rows
					lineArray[i] = line.split(" ");
					i++;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Can not read the line!");
		}
		//Initializing n
		Solution.n = lineArray[0].length;
		//Initializing distances lookup table
		Solution.distanceLookupTable = new int[Solution.n][Solution.n];
		
		//Converting elements of the distances lookup table to int
		for(int j=0; j<Solution.n;j++)
		{
			for(int k=0; k<Solution.n; k++)
			{
				Solution.distanceLookupTable[j][k] = Integer.valueOf(lineArray[j][k]).intValue();
			}
		}
		
		// Start the Search:
		Solution sol = new Solution(Solution.n);
		sol.output(true, false, true);
		//Solution sol2 = sol.copy();
		//sol2.plusOne();
		//sol2.output(true, false, true);
		System.out.println("Start search...");
		sol = new Solution(Solution.n);
		new Search(Search.BEST_IMPROVEMENT, 1000, sol, 1.0, Solution.n);
	}
}
